package com.hogan.basiclibrary.base.ui.hoganGlide;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.hogan.basiclibrary.R;


/**
 * Created by chenhu on 2017/11/21.
 * 自定义圆形进度条
 */

public class CustomProgressBar extends View {
    private Paint mCirclePaint;//实心圆画笔

    private Paint mRingPaint;//圆环画笔

    private Paint mRingPaintBg;//圆环背景色画笔

    private Paint mTextPaint;//字体画笔

    private int mCircleColor;//实心圆颜色

    private int mRingColor;//圆环颜色

    private int mRingColorBg;//圆环背景色颜色

    private float mRadius;//圆半径

    private float mRingRadius;//圆环半径

    private float mStrokeWidth;//圆环宽度

    private int mXCenter;//圆心X坐标

    private int mYCenter;//圆心Y坐标

    private float mTextWidth;//字的长度

    private float mTextHeight;//字的高度

    private int mTotalProgress = 100;//总进度

    private int mProgress;//当前进度


    public CustomProgressBar(Context context) {
        this(context, null);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        initView();
    }

    private void initAttrs(Context context, AttributeSet attributeSet) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributeSet
                , R.styleable.CustomProgressBar, 0, 0);
        mRadius = typedArray.getDimension(R.styleable.CustomProgressBar_radius, 100);
        mStrokeWidth = typedArray.getDimension(R.styleable.CustomProgressBar_strokeBarWidth, 8);
        mCircleColor = typedArray.getColor(R.styleable.CustomProgressBar_circleColor, 0xFFFFFFFF);
        mRingColor = typedArray.getColor(R.styleable.CustomProgressBar_ringColor, 0xFFFFFFFF);
        mRingColorBg = typedArray.getColor(R.styleable.CustomProgressBar_ringBgColor, 0xFFFFFFFF);
        mRingRadius = mRadius + mStrokeWidth / 2;
        typedArray.recycle();
    }

    private void initView() {
        //实心圆画笔
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        //圆环画笔
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);

        //圆环背景色画笔
        mRingPaintBg = new Paint();
        mRingPaintBg.setAntiAlias(true);
        mRingPaintBg.setColor(mRingColorBg);
        mRingPaintBg.setStyle(Paint.Style.STROKE);
        mRingPaintBg.setStrokeWidth(mStrokeWidth);

        //字体画笔
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);
        mTextPaint.setTextSize(mRadius / 2);

        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = (int) Math.ceil(fontMetrics.descent - fontMetrics.ascent);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mXCenter = getWidth() / 2;
        mYCenter = getHeight() / 2;
        //实心圆
        canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
        //圆环
        RectF oval = new RectF();
        oval.left = mXCenter - mRingRadius;
        oval.top = mYCenter - mRingRadius;
        oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
        oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
        //圆弧所在的椭圆对象、圆弧的起始角度、圆弧的角度、是否显示半径连线
        canvas.drawArc(oval, 0, 360, false, mRingPaintBg);

        //字体
        String text = mProgress + "%";
        mTextWidth = mTextPaint.measureText(text, 0, text.length());
        //  canvas.drawText(text, mXCenter - mTextWidth / 2, mYCenter - mTextHeight / 8, mTextPaint);
        canvas.drawText(text, mXCenter - mTextWidth / 2, mYCenter + mTextHeight / 8, mTextPaint);

        //圆环背景
        if (mProgress > 0) {
            RectF rectF = new RectF();
            rectF.left = mXCenter - mRingRadius;
            rectF.top = mYCenter - mRingRadius;
            rectF.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            rectF.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            float arc = (mProgress * 360) / mTotalProgress;
            canvas.drawArc(rectF, -90, arc, false, mRingPaint); //
        }
    }

    public synchronized void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (progress > mTotalProgress) {
            progress = mTotalProgress;
        }
        if (progress <= mTotalProgress) {
            this.mProgress = progress;
            postInvalidate();
        }
    }
}
